package com.Adapter;

import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bolao.R;
import com.bolao.teamFragments.TeamTabsActivity;
import com.entidades.Group;
import com.entidades.Team;

public class GroupAdapter extends BaseAdapter {


	private List<Group> groups;
	private LayoutInflater mInflater;
	private Context mContext;
	private Team selectedTeam;
	
	static class ViewHolder{
		private TextView groupName;
		private ImageView team1;
		private ImageView team2;
		private ImageView team3;
		private ImageView team4;
	
	}
	

	public GroupAdapter(Context context, List<Group> groups){
		this.groups = groups;
		mInflater = LayoutInflater.from(context);
		mContext = context;
	}



	@Override
	public int getCount() {
		return groups.size();
	}




	@Override
	public Object getItem(int arg0) {
		
		return groups.get(arg0);
	}




	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}




	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.group_parent, null);
			holder = new ViewHolder();  
			holder.groupName = (TextView) convertView.findViewById(R.id.textViewGroupName);
			holder.team1 = (ImageView) convertView.findViewById(R.id.imageViewTeam1);
			holder.team2 = (ImageView) convertView.findViewById(R.id.imageViewTeam2);
			holder.team3 = (ImageView) convertView.findViewById(R.id.imageViewTeam3);
			holder.team4 = (ImageView) convertView.findViewById(R.id.imageViewTeam4);
			
            convertView.setTag(holder);
                  
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Resources r = mContext.getResources();		
		Group group = groups.get(position);
		holder.groupName.setText(String.valueOf(group.getId()));
		List<Team> teams = group.getTeams();
		
		holder.team1.setImageResource(r.getIdentifier(teams.get(0).getFlag(), "drawable",  mContext.getPackageName()));
		holder.team2.setImageResource(r.getIdentifier(teams.get(1).getFlag(), "drawable",  mContext.getPackageName()));
		holder.team3.setImageResource(r.getIdentifier(teams.get(2).getFlag(), "drawable",  mContext.getPackageName()));
		holder.team4.setImageResource(r.getIdentifier(teams.get(3).getFlag(), "drawable",  mContext.getPackageName()));
		
		holder.groupName.setText(String.valueOf(group.getId()));

		holder.team1.setOnClickListener(new OnTeamClickListener(teams.get(0)));
		holder.team2.setOnClickListener(new OnTeamClickListener(teams.get(1)));
		holder.team3.setOnClickListener(new OnTeamClickListener(teams.get(2)));
		holder.team4.setOnClickListener(new OnTeamClickListener(teams.get(3)));
		
		return convertView;
	}
	

	public Team getSelectedTeam() {
		return selectedTeam;
	}



	public void setSelectedTeam(Team selectedTeam) {
		this.selectedTeam = selectedTeam;
	}

	private class OnTeamClickListener extends AsyncTask<Void, String, Void>implements OnClickListener {
		private ProgressDialog progressBar;
		private Team team;
		public OnTeamClickListener(Team team){
			super();
			this.team = team;
		}
		
		@Override
		public void onClick(View view) {
			
			
			this.execute();

		}
		
		@Override
		protected void onPreExecute() {
			
			progressBar = new ProgressDialog(mContext);
			progressBar.setCancelable(true);
			progressBar.setMessage(mContext.getString(R.string.loading));
			progressBar.setProgress(0);
			progressBar.setMax(100);
			progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressBar.show();
			
			
	 
	              
			
		};

		@Override
		protected Void doInBackground(Void... params) {
			TeamTabsActivity.setTeam(team);
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {

			progressBar.dismiss();			
			Intent i = new Intent(mContext, TeamTabsActivity.class);
			mContext.startActivity(i);
			super.onPostExecute(result);
		}
		
	}

}
